﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data;

/// <summary>
///ConfigGlobal 的摘要说明
/// </summary>
public class ConfigGlobal
{
	public ConfigGlobal()
	{
              
	}

    public static void CheckLog()
    {
        if (!ConfigGlobal.IsLog())
        {
            //HttpContext.Current.Response.Redirect(ConfigGlobal.GetGlobal().AdminDir);
            //Helper.WriteScriptAtBottom("top.location.href='" + ConfigGlobal.GetGlobal().AdminDir + "'", "nolog");
            //Helper.WriteScriptAtTop("top.location.href='" + ConfigGlobal.GetGlobal().AdminDir + "'", "nolog");
            //Helper.WriteScriptByLit("top.location.href='" + ConfigGlobal.GetGlobal().AdminDir + "'");
            string path =HttpContext.Current.Request.ApplicationPath+"/"+ ConfigGlobal.GetGlobal().AdminDir.Replace("~/","");            
            HttpContext.Current.Response.Write("<script>top.location.href='" + path + "'</script>");
            HttpContext.Current.Response.End();
        }
    }

    public static ConfigGlobalSettings GetGlobal()
    {
        string path = HttpContext.Current.Server.MapPath("~/configs/ConfigGlobalSettings.xml");
        if (!File.Exists(path))
        {
            new ConfigGlobalSettings().ObjToXml(path);
        }

        return path.XmlToObj<ConfigGlobalSettings>();
    }

    public static DataTable GetAllRoles()
    {
        ConfigGlobalSettings gSetting = ConfigGlobal.GetGlobal();
        if (gSetting.AllowRole)
        {
            string sql = "select * from " + gSetting.RoleTableName;
            DataTable tab = DBFactory.GetConn().exeTable(sql);
            return tab;
        }
        else
        {
            return null;
        }
    }

    public static DataTable GetAllAdmins()
    {
        ConfigGlobalSettings gSetting = ConfigGlobal.GetGlobal();
        if (gSetting.AllowBrowseControl)
        {
            string sql = "select * from " + gSetting.AdminTableName;
            return DBFactory.GetConn().exeTable(sql);
        }
        else
        {
            return null;
        }
    }

    public static bool CheckLogin(string uname,string passwd)
    {
        ConfigGlobalSettings gSetting = ConfigGlobal.GetGlobal();
        ConfigTable adminTable=ConfigTable.GetDefaultConfig(gSetting.AdminTableName);
        string sql = "select * from " + gSetting.AdminTableName + " where " + gSetting.UserNameColumnField + "=" + DataTypeFactory.GetSqlValue(uname, adminTable[gSetting.UserNameColumnField].ColumnDataType);
        DataRow row = DBFactory.GetConn().exeRow(sql);

        if (row != null)
        {
            if (row[gSetting.PasswordColumnField].ToString() == passwd)
            {
                HttpResponse Response = HttpContext.Current.Response;
                Response.Cookies.Add(new HttpCookie("admin_uid",row[gSetting.AdminPKColumnField].ToString()));
                Response.Cookies.Add(new HttpCookie("admin_uname",row[gSetting.UserNameColumnField].ToString()));
                if (gSetting.AllowRole)
                {
                    Response.Cookies.Add(new HttpCookie("admin_roleid",row[gSetting.RoleColumnField].ToString()));
                }
                return true;
            }
            else
            {
                throw new WrongPasswordException("密码不正确");
            }
        }
        else
        {
            throw new NoUserException("用户名不正确");
        }
    }

    public static bool IsLog()
    {
        if (HttpContext.Current.Request.Cookies["admin_uid"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["admin_uid"].Value))
        {
            return true;
        }
        else
        {            
            return false;
        }
    }

    public static bool IsLog(out string uid)
    {
        if (HttpContext.Current.Request.Cookies["admin_uid"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["admin_uid"].Value))
        {
            uid = HttpContext.Current.Request.Cookies["admin_uid"].Value;
            return true;
        }
        else
        {
            uid = string.Empty;
            return false;
        }
    }

    public static bool IsOldPassRight(string OldPass)
    {
        string uid = string.Empty;
        if (ConfigGlobal.IsLog(out uid))
        {
            string sql = "select " + ConfigGlobal.GetGlobal().PasswordColumnField + " from " + ConfigGlobal.GetGlobal().AdminTableName + " where " + ConfigGlobal.GetGlobal().AdminPKColumnField + "=" + DataTypeFactory.GetSqlValue(uid, ConfigTable.GetDefaultConfig(ConfigGlobal.GetGlobal().AdminTableName)[ConfigGlobal.GetGlobal().AdminPKColumnField].ColumnDataType);

            string dbPass = DBFactory.GetConn().exe1(sql);
            if (dbPass.ToLower().Trim() == OldPass.ToLower().Trim())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            throw new NotLogOnException("尚未登陆");
        }
    }

    public static void UpdatePassWord(string newPass)
    {
        string uid = string.Empty;
        if (ConfigGlobal.IsLog(out uid))
        {
            string sql = "update " + ConfigGlobal.GetGlobal().AdminTableName + " set " + ConfigGlobal.GetGlobal().PasswordColumnField + "=" + DataTypeFactory.GetSqlValue(newPass, ConfigTable.GetDefaultConfig(ConfigGlobal.GetGlobal().AdminTableName)[ConfigGlobal.GetGlobal().PasswordColumnField].ColumnDataType) + " where " + ConfigGlobal.GetGlobal().AdminPKColumnField + "=" + DataTypeFactory.GetSqlValue(uid, ConfigTable.GetDefaultConfig(ConfigGlobal.GetGlobal().AdminTableName)[ConfigGlobal.GetGlobal().AdminPKColumnField].ColumnDataType);
            DBFactory.GetConn().exe0(sql);
        }
        else
        {
            throw new NotLogOnException("尚未登陆");
        }
    }
}
